package com.lelefly.base.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;











import org.springframework.web.bind.annotation.ResponseBody;

import com.lelefly.Log.Logger;
import com.lelefly.base.Validator.LeleflyUserValidator;
import com.lelefly.base.model.LeleflyUserVo;
import com.lelefly.base.model.Result;
import com.lelefly.base.model.ResultVo;
import com.lelefly.base.po.FarmlandInfo;
import com.lelefly.base.po.LeleflyUser;
import com.lelefly.base.serviceImp.FarmlandInfoService;
import com.lelefly.base.serviceImp.LeleflyUserService;
import com.lelefly.base.utils.DateUtils;
import com.lelefly.base.utils.LUUIDutils;
import com.lelefly.base.utils.ParamsUtils;
import com.lelefly.handleexecption.ValidateExecption;


@Controller
@RequestMapping("/farminfo")
public class FarmlandInfoController extends BaseController{

	@Resource
	public FarmlandInfoService farmlandInfoService;
	
	@Resource
	public LeleflyUserValidator leleflyUserValidator;
	
	@Resource
	private JdbcTemplate jdbcTemplate;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/getFarmlandInfo",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result getFarmlandInfos(String usernm){
		Result<LeleflyUserVo> result=null;
		try {
			result = farmlandInfoService.getFarmlandInfos(usernm);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getLogger().info("----->>"+e.getLocalizedMessage());
		}
		return result;
	}
	
	
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/addFarmlandInfo",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result addFarmlandInfo(FarmlandInfo farmlandInfo){
		Result<FarmlandInfo> result=null;
		try {
			if (StringUtils.isEmpty(farmlandInfo.getFarmlandnm())) {
				farmlandInfo.setFarmlandnm(LUUIDutils.getUUID());
			}
			result = farmlandInfoService.addFarmlandInfo(farmlandInfo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getLogger().info("----->>"+e.getLocalizedMessage());
		}
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/updateFarmlandInfo",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result updateFarmlandInfo(FarmlandInfo farmlandInfo){
		Result<FarmlandInfo> result=null;
		try {
			result = farmlandInfoService.updateFarmlandInfo(farmlandInfo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getLogger().info("----->>"+e.getLocalizedMessage());
		}
		return result;
	}
	

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/updatefarmlandname",produces="application/json;charset=utf-8")
	@ResponseBody
	public ResultVo updateFarmlandname(String nm , String name){
		ResultVo result  = new ResultVo();
		result.setStatus("false");
		String sql = "select count(*) from farmland_info where farmlandnm =?";
		String updatesql = "update farmland_info set farmlandname = ?  where farmlandnm =?";
		try {
			ParamsUtils.checkParams(nm,name);
			int  count= jdbcTemplate.queryForInt(sql, new Object[]{nm});
			if (count==0) {
				throw new ValidateExecption("要修改的田块不存在");
			}
          
            jdbcTemplate.update(updatesql,name,nm);
            
			result.setStatus("true");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			getLogger().info("----->>"+e.getLocalizedMessage());
		}catch (ValidateExecption e) {
			// TODO: handle exception
			result.setError(e.mes);
		}
		return result;
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/delFarmlandInfo",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result delFarmlandInfo(String farmlandid){
		Result<FarmlandInfo> result=null;
		try {
			result = farmlandInfoService.delFarmlandInfo(farmlandid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getLogger().info("----->>"+e.getLocalizedMessage());
		}
		return result;
	}
	
	
}
